<?php
/**
 * Content controller
 * @author Antonello D'Ippolito (http://www.antonellodippolito.com)
 */

class ContentController extends Zend_Controller_Action {
   
    /**
     * Renders the content index page
     */
    public function indexAction() {
        $this->view->pageTitle = $GLOBALS["lang"]->_("content_list");
        $content = new Content();
        $this->view->pagesList = $content->listPages();
    }
    
    /**
     * Renders the edit content page
     * @todo Verifica errore -AD
     */
    public function editAction() {
        $this->view->pageTitle = $GLOBALS["lang"]->_("content_edit");
        $id = $this->_request->getParam('id');
        $content = new Content();
        $this->view->page = $content->read($id);
        if($this->view->page==null) {
            throw new ErrorController("NO_CONTENT");
        }
    }
    
    /**
     * Renders the new content page
     */
    public function newAction() {
        $this->view->pageTitle = $GLOBALS["lang"]->_("content_new");
    }
    
    /**
     * Saves the content of a page without rendering (AJAX call)
     */
    public function saveAction() {
        
        $this->_helper->layout()->disableLayout();
        Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
        
        $id = $this->_request->getParam('id');
        $title = $this->_request->getParam('title');
        $content = $this->_request->getParam('content');
        $data = array("id" => $id, "title" => $title, "content" => $content);
        
        $content = new Content();
        if($content->save($data)) {
            echo "<div class='dataora'>".date("d-m-y H.i.s")."</div><br>".
                 $GLOBALS["lang"]->_("content_saved");
        }
        else {
            echo "<div class='dataora'>".date("d-m-y H.i.s")."</div><br>".
                 $GLOBALS["lang"]->_("content_saved_error");
            error_log(__METHOD__.__LINE__." Error saving content, data = ".print_r($data,true));
        }
    }
} 
?>
